package utils

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"
)

type ReceivedBody struct {
	Platform     []string     `json:"platform"`
	Audience     Audience     `json:"audience"`
	Notification Notification `json:"notification"`
	Options      interface{}  `json:"options"`
}

type Notification struct {
	IOS     IOS     `json:"ios"`
	Android Android `json:"android"`
}

//根据ID推动
//type Audience struct {
//	Alias []string `json:"alias"`
//}
//根据tag推动
type Audience struct {
	Tag []string `json:"tag"`
}
type IOS struct {
	Alert  string `json:"alert"`
	Sound  string `json:"sound"`
	Badge  string `json:"badge"`
	Extras Extras `json:"extras"`
}

type Android struct {
	Alert string `json:"alert"`
	Title string `json:"title"`
}

type Extras struct {
	Newsid int `json:"newsid"`
}

/**
* 收币通知
* https://api.jpush.cn/v3/push 极光推送
* @uid 用户id
* @coinSx 币种简称
* @amount 接收金额
* @transferId
 */
func JPushReceived(msg string, uid int) {
	var tag = ""

	if uid == 1 {
		tag = "a"
	} else {
		tag = "b"
	}
	if beego.BConfig.RunMode == "dev" {
		msg = "测试:" + msg
	}
	var url = "https://api.jpush.cn/v3/push"
	post := httplib.Post(url)
	post.Header("Authorization", "Basic "+beego.AppConfig.String("jpush_Authorization"))
	options := map[string]interface{}{
		"apns_production": false,
	}
	body := ReceivedBody{
		Platform: []string{"android", "ios"},
		Audience: Audience{Tag: []string{tag}},
		Notification: Notification{
			IOS: IOS{
				Alert: msg,
				Sound: "default",
				Badge: "+1",
			},
			Android: Android{
				Alert: msg,
				Title: "wlt后台管理app",
			}},

		Options: options,
	}
	post.JSONBody(body)
	post.DoRequest()

}
